//
//  LSVisitorView.swift
//  新浪微博---01次
//
//  Created by 李宗帅 on 16/5/3.
//  Copyright © 2016年 LS. All rights reserved.
//

import UIKit
import SnapKit

/**
 代理的实现
 */
protocol LSVistorViewDelegate: NSObjectProtocol{
    
    func registerDidMethod()
    
    func loginDidMethod()
}
class LSVisitorView: UIView {

    /**
    *  自定义的三部曲
    *  1.重写父类的构造方法
    *  2.懒加载
    *  3.添加子控件和设置约束
    */
     
    //定义一个闭包属性  方便按钮点击的时候调用
    var registerClosure: (() -> ())?
    
    var loginClosure: (() -> ())?
    

    //添加代理属性
    weak var __delegate: LSVistorViewDelegate?
    
    
    //第一步,  重写父类的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    //第三步,  添加子控件  和设置约束
    private func setupUI(){
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
        //添加到当前view
        
        addSubview(turnImageView)
        
        addSubview(maskImageView)
        
        addSubview(bgImageView)

        addSubview(noticeLabel)
        
        addSubview(registreButton)
        
        addSubview(loginButton)
        
        
        //设置约束
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        
        }
        
        //居中
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //另一种居中方式
        turnImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            
            make.centerY.equalTo(self.snp_centerY)
        }
        
        //其实设置约束有一个规律: 水平居中,垂直居中,左,上,右,下,宽,高: 一个控件不超过4个约束
        //设置Label约束:水平居中,上,宽
        noticeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            
            make.top.equalTo(turnImageView.snp_bottom).offset(10)
            
            make.width.equalTo(216)
        }
        
        //注册按钮
        registreButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(noticeLabel.snp_left)
            
            make.top.equalTo(noticeLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            
            make.height.equalTo(30)
        }
        
        //登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(noticeLabel.snp_right)
            
            make.top.equalTo(noticeLabel.snp_bottom).offset(10)
            
            make.width.equalTo(registreButton.snp_width)
            
            make.height.equalTo(registreButton.snp_height)
        }
    }
    
    //对外提供一个接口(方法),在方法中可以修改私有的属性
    func setupInfo(title: String,image: String?){

        
        if let image = image {
            //非首页
            noticeLabel.text = title
            
            bgImageView.image = UIImage(named: image)
            
            //隐藏转动动画
            turnImageView.hidden = true
        } else {
            //首页 不隐藏转动动画
            turnImageView.hidden = false
            
            startAnimation()

        }
        //需要把首页的转动图片隐藏
        //我们需要知道首页传过来的数据
         
    }
    //按钮的点击事件
    @objc private func showRegister(){
        
        //用闭包实现回调
        registerClosure?()
        
        __delegate?.registerDidMethod()
    }
    
    @objc private func showLogin(){
        
        loginClosure?()
        
        __delegate?.loginDidMethod()
        
    }
    
    //第二步,  懒加载
    
    //遮罩
    private lazy var maskImageView: UIImageView = {
        let img = UIImageView()
        
//        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        img.image = UIImage(named: "abc")
 
        return img
    }()
    
    //中心视图
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
    }()
    
    //转动视图
    private lazy var turnImageView: UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
    }()
    
    //提示Label
    private lazy var noticeLabel: UILabel = {
        let label = UILabel()
        
        label.textAlignment = .Center
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.text = "关注一些人,回这里看看有什么惊喜?"
        
        label.numberOfLines = 0
        
        return label
    }()
    
    //注册按钮
    private lazy var registreButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "showRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /**
    *  登录按钮
    */
    private lazy var loginButton: UIButton = {
       
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "showLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

    /**
    *  实现动画转动
    */
    func startAnimation(){
        //keyPath  可以修改可选属性的属性
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.duration = 10
        
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        //切换界面的时候 动画会被释放,指定为 false之后切换界面动画就不会释放
        animation.removedOnCompletion = false
        
        turnImageView.layer.addAnimation(animation, forKey: nil)
    }
}
